package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Employee;
import org.example.entity.Offermanagementtable;
import org.example.models.R;
import org.example.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/into")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/find")
    public R findAll(Page<Employee> page) {
        return R.success(employeeService.page(page));
    }
    @GetMapping("/findPage")
    public R<Page<Employee>> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        Page<Employee> page = new Page<>(pageNum, pageSize);

        if (keyword != null && !keyword.isEmpty()) {
            com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Employee> queryWrapper =
                    new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>();
            queryWrapper.like("employee_name", keyword)
                    .or()
                    .like("employee_id_number", keyword);
            Page<Employee> resultPage = employeeService.page(page, queryWrapper);

            // 隐藏密码信息
            resultPage.getRecords().forEach(employee -> {
                if (employee.getLoginPassword() != null) {
                    employee.setLoginPassword("******");
                }
            });
            return R.success(resultPage);
        }
        Page<Employee> resultPage = employeeService.page(page);
        // 隐藏密码信息
        resultPage.getRecords().forEach(employee -> {
            if (employee.getLoginPassword() != null) {
                employee.setLoginPassword("******"); // 用星号代替真实密码
            }
        });
        return R.success(resultPage);
    }
    @PutMapping("/updateStatus")
    public R updateEmployeeStatus(@RequestParam Integer employeeId, @RequestParam String status) {
        Employee employee = employeeService.getById(employeeId);
        if (employee == null) {
            return R.error("员工不存在");
        }

        employee.setEmployeeStatus(status);
        boolean updated = employeeService.updateById(employee);
        if (updated) {
            return R.success("员工状态更新成功");
        } else {
            return R.error("员工状态更新失败");
        }
    }
    @PostMapping("/update")
    public R updateEmployee(@RequestBody Employee employee) {
        boolean updated = employeeService.updateById(employee);
        if (updated) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }
    @PostMapping("/id")
    public R findById(@RequestBody(required = false) IdRequest request,
                      @RequestParam(required = false) Integer id) {
        Integer userId = (request != null) ? request.getId() : id;
        if (userId == null) {
            return R.error("用户ID不能为空");
        }
        return R.success(employeeService.getById(userId));
    }
    @PostMapping("/add")
    public R<String> addEmployee(@RequestBody Employee employee) {
        try {
            // 检查工号是否已存在
            QueryWrapper<Employee> idQueryWrapper = new QueryWrapper<>();
            idQueryWrapper.eq("employee_id_number", employee.getEmployeeIdNumber());
            Employee existingEmployeeById = employeeService.getOne(idQueryWrapper);

            if (existingEmployeeById != null) {
                return R.error("员工已入职");
            }

            // 检查身份证号是否已存在
            QueryWrapper<Employee> numberQueryWrapper = new QueryWrapper<>();
            numberQueryWrapper.eq("employee_number", employee.getEmployeeNumber());
            Employee existingEmployeeByNumber = employeeService.getOne(numberQueryWrapper);

            if (existingEmployeeByNumber != null) {
                return R.error("员工已入职");
            }

            boolean saved = employeeService.save(employee);
            if (saved) {
                return R.success("添加成功");
            } else {
                return R.error("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("添加失败: " + e.getMessage());
        }
    }
    @GetMapping("/checkEmployeeId")
    public R<Map<String, Object>> checkEmployeeId(@RequestParam String employeeIdNumber) {
        Map<String, Object> result = new HashMap<>();

        // 只检查工号
        QueryWrapper<Employee> idQueryWrapper = new QueryWrapper<>();
        idQueryWrapper.eq("employee_id_number", employeeIdNumber);
        Employee existingEmployeeById = employeeService.getOne(idQueryWrapper);

        boolean exists = (existingEmployeeById != null);

        result.put("exists", exists);
        return R.success(result);
    }
    public static class IdRequest {
        private Integer id;

        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
    }
    //根据id查询员工信息

    @GetMapping("/getById")
    public R<Employee> getById(@RequestParam("id") Integer id){
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }
    @GetMapping("/getByIds")
    public R<Employee> getById(@RequestParam("id") String id){
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }
}
